package com.bluefeather.songlyric;

import android.app.ListFragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ToneList extends ListFragment{
	
	public static String	TAG	= "TONE";
	
	private int				_type;
	private String[]		_columns = new String[] {  DB.TONE._TITLE, DB.TONE._SUBJECT };
	private int[]			_ids = new int[] { R.id.tone_title_text, R.id.tone_subject };

	static ToneList newInstance(int menuId) {
		ToneList tl = new ToneList();
		Bundle args = new Bundle();
		int type = menuId == R.string.drawer_tone_p ? DB.TONE_TYPE_P :
			menuId == R.string.drawer_tone_z ? DB.TONE_TYPE_Z : DB.TONE_TYPE_R;
		args.putInt("type", type);
		tl.setArguments(args);
		return tl;
	}
	
	// it seems Android will call empty constructor and use ugly setArguments/getArguments
	public ToneList() {}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_type = getArguments() != null ? getArguments().getInt("type") : DB.TONE_TYPE_P;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
		View view = inflater.inflate(R.layout.fragment_music_list, container, false);
		view.setBackgroundColor(0xffffffff);
		Cursor c = DB.instance().getToneList(_type);
		setListAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_tone, c, _columns, _ids, 0));
		return view;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		SongLyricActivity activity = (SongLyricActivity)getActivity();
		int toneIndex = DB.instance().getToneIndex((int)id);
		activity.openTone(toneIndex, _type);
	}
	
}
